var client = {};
var app = getApp();

// 房间详情页取消收藏接口
client.cancelCollection = function (houseId, that) {
  console.log("houseId: " + houseId);
  my.request({
    url: app.globalData.url + "c11ce533-2b21-4e86-bec1-3646d3c6d636",
    method: "POST",
    data: {
      houseId: houseId
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      if (res.data.succeed == 0) {
        my.showToast({
          content: "取消收藏成功",
          duration: 2000
        });
        that.setData({
          isCollected: false,
          collectionIcon: "../../image/collection.png",
          collectionText: "添加收藏"
        });
      } else {
        app.errorHandling("取消收藏失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 房间详情页添加收藏接口
client.addCollection = function (houseId, that) {
  my.request({
    url: app.globalData.url + "a3daaa13-d27b-44af-99bb-1d694f4f7112",
    method: "POST",
    data: {
      houseId: houseId
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      console.log(JSON.stringify(res));
      if (res.data.succeed == 0) {
        my.showToast({
          content: "收藏成功",
          duration: 2000
        });
        that.setData({
          isCollected: true,
          collectionIcon: "../../image/collected.png",
          collectionText: "取消收藏"
        });
      } else {
        app.errorHandling("收藏失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () { }
  });
};

// 提交认购申请接口（锁定房源）
client.submitBuy = function (houseId, that) {
  console.log("secondClick   ", that.data.secondClick);
  // 防止连续点击
  if (!that.data.secondClick) {
    return;
  }
  that.setData({
    secondClick: false
  });
  my.showLoading({
    content: '加载中...'
  });
  my.request({
    url: app.globalData.url + "7505a084-77a8-464c-b688-62e80846abf8",
    method: "POST",
    data: {
      houseId: houseId
    },
    headers: {
      "Access-Token": app.globalData.token
    },
    // 期望返回的数据格式，默认json，支持json，text，base64
    dataType: "json",
    // 调用成功的回调函数
    success: function (res) {
      if (res.data.succeed == 0) {
        my.showToast({
          content: "提交认购申请成功",
          duration: 2000
        });
        my.setStorageSync({
          key: 'lockedHouseId',
          data: houseId
        });

        my.redirectTo({
          url: "../confirmBuy/confirmBuy?houseId=" + houseId
        });
        that.setData({
          modalOpened: false
        });
      } else {
        app.errorHandling("提交认购申请失败：", res);
      }
    },
    // 调用失败的回调函数
    fail: function (res) {
      my.showToast({
        content: "网络错误：" + res.data.errorMsg,
        duration: 2000
      });
    },
    complete: function () {
      my.hideLoading();
      that.setData({
        secondClick: true
      });
    }
  });
};

module.exports = {
  client: client
};